const connection = require('../data');
const fs = require('fs');

// 显示创建班级页面
module.exports.register = (req, res) => {
    connection.query('select * from class', (error, result) => {
        //    判断是否登陆 未登录就跳转到登陆页面
        if (!req.session.login) {
            res.redirect('/login');
            return;
        }
        if (error) {
            console.log(error)
        } else {
            res.render('register', result[0])
        }
    })

}
// 上传头像图片
module.exports.postAddUpload = (req, res) => {
    fs.writeFile('./public/upload/' + req.file.originalname, req.file.buffer, (error) => {
        if (error) {
            console.log(error)
        }
        res.json({
            picAdd: './upload/' + req.file.originalname
        })
    })

}
// 创建班级页面
module.exports.AddGrade = (req, res) => {
    var str = [req.body.school_name, req.body.class_chinesename, req.body.class_englishname, req.body.class_logo, req.session.user[0].users_id, null]
    let = users_idData = req.session.user.users_id
    connection.query('insert into class(school_name,class_chinesename,class_englishname,class_logo,teacher_id,class_creationtime) values(?,?,?,?,?,?)', str, (error, result) => {
        if (error) {
            console.log(error)
        } else {
            if (result) {
                res.json({
                    code: "1003",
                    msg: '班级创建成功',
                })
            }
        }
    })
}